package com.micangcaihang.micang.mvp.task.ui;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;

import com.micangcaihang.common.base.fragment.BaseFragment;
import com.micangcaihang.common.utils.Properties;
import com.micangcaihang.common.utils.StringUtils;
import com.micangcaihang.common.utils.TimeUtils;
import com.micangcaihang.common.utils.ToastUtils;
import com.micangcaihang.micang.R;
import com.micangcaihang.micang.databinding.FragmentTaskLandInfoBinding;
import com.micangcaihang.micang.entity.TaskLandInfoEntity;
import com.micangcaihang.micang.http.request.TaskLandInfoRequest;
import com.micangcaihang.micang.mvp.task.contract.TaskLandInfoContract;
import com.micangcaihang.micang.mvp.task.presenter.TaskLandInfoPresenter;
import com.micangcaihang.micang.view.PickerViewHelper;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

/**
 * <pre>
 *     author : Fandy
 *     e-mail : fandayongfly@gmail.com
 *     time   : 2017/07/24
 *     update : 2017/07/24
 *     desc   : 土地信息
 *     version: 1.0
 * </pre>
 */

public class TaskLandInfoFragment extends BaseFragment<TaskLandInfoPresenter,
        FragmentTaskLandInfoBinding> implements TaskLandInfoContract.View {
    private String id;
    private TaskLandInfoRequest mRequest;
    private TaskLandInfoRequest mOldRequest;
    private boolean isNetSuccess;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        id = getArguments().getString("id");
    }

    public static TaskLandInfoFragment newInstance(String id) {
        Bundle args = new Bundle();
        TaskLandInfoFragment fragment = new TaskLandInfoFragment();
        args.putString("id", id);
        fragment.setArguments(args);
        return fragment;
    }

    /**
     * 获取资源文件布局
     *
     * @return 资源布局文件layout
     */
    @Override
    protected int getLayoutId() {
        return R.layout.fragment_task_land_info;
    }

    @Override
    public void initPresenter() {
        super.initPresenter();
        mPresenter.setId(id);
    }

    @Override
    protected void initData() {
        super.initData();
        mRequest = new TaskLandInfoRequest();
        mRequest.setId(id);
        mOldRequest = new TaskLandInfoRequest();
        mOldRequest.setId(id);
    }

    /**
     * 初始化按钮监听
     */
    @Override
    protected void initListener() {
        mViewBinding.setClickHandler(new ClickHandler());
    }

    /**
     * 返回土地信息信息
     *
     * @param entity
     */
    @Override
    public void returnLandInfoData(TaskLandInfoEntity entity) {
        isNetSuccess = true;
        String[] excludsArray = new String[]{"choose_list", "id"};
        try {
            Properties.copyPropertiesExclude(entity, mRequest, excludsArray);
            Properties.copyPropertiesExclude(entity, mOldRequest, excludsArray);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (!StringUtils.isEmpty(entity.getLand_position())) {
            mViewBinding.etLandPosition.setText(entity.getLand_position());
        }
        if (!StringUtils.isEmpty(entity.getLand_use())) {
            mViewBinding.etLandUse.setText(entity.getLand_use());
        }
        if (!StringUtils.isEmpty(entity.getLandl_certificate_number())) {
            mViewBinding.etLandlCertificateNumber.setText(entity.getLandl_certificate_number());
        }
        if (!StringUtils.isEmpty(entity.getLand_end_date())) {
            String time = TimeUtils.millis2StringDay(Long.parseLong(entity.getLand_end_date()) * 1000);
            mViewBinding.tvLandEndDate.setText(time);
        }
        if (!StringUtils.isEmpty(entity.getLand_use_type())) {
            mViewBinding.etLandUseType.setText(entity.getLand_use_type());
        }
        if (!StringUtils.isEmpty(entity.getLand_area())) {
            mViewBinding.etLandArea.setText(entity.getLand_area());
        }
    }

    public class ClickHandler {
        public void onSave(View view) {
            saveData();
        }

        public void onShowLandEndDatePickerView(View view) {
            Calendar startDate = Calendar.getInstance();
            startDate.set(1955, 0, 1);
            Calendar endData = Calendar.getInstance();//系统当前时间
            Calendar selectData = Calendar.getInstance();
            if (!StringUtils.isEmpty(mRequest.getLand_end_date())) {
                selectData.setTime(TimeUtils.millis2Date(Long.parseLong(mRequest.getLand_end_date()) * 1000));
            }
            PickerViewHelper.showYearMonthDayPicker(_mActivity, "终止日期", startDate, endData, selectData, new PickerViewHelper.OnTimeSelectListenner() {
                @Override
                public void onTimeSelect(Date date) {
                    String date2String = TimeUtils.date2String(date, new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault()));
                    mViewBinding.tvLandEndDate.setText(date2String);
                    String checkin_date = String.valueOf(TimeUtils.string2Millis(date2String, new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault())) / 1000);
                    mRequest.setLand_end_date(checkin_date);
                }
            });
        }
    }

    /**
     * 开始保存数据请求
     */
    public void saveData() {
        setSaveData();
        if (mRequest.toString().equals(mOldRequest.toString())) {
            ToastUtils.showLong("您没有更改任何信息，无需保存");
            return;
        }
        isNetSuccess = false;
        mPresenter.saveLandInfoData(mRequest);
    }

    /**
     * 判断是否保存过数据 没有的话点击保存去保存
     *
     * @return 是否保存当前页面数据
     */
    public boolean isSaveData() {
        //如果请求错误则返回true
        if (!isNetSuccess) {
            return true;
        }
        setSaveData();
        if (mRequest.toString().equals(mOldRequest.toString())) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 设置请求数据
     */
    private void setSaveData() {
        mRequest.setLand_position(mViewBinding.etLandPosition.getText().toString().trim());
        mRequest.setLand_use(mViewBinding.etLandUse.getText().toString().trim());
        mRequest.setLandl_certificate_number(mViewBinding.etLandlCertificateNumber.getText().toString().trim());
        mRequest.setLand_use_type(mViewBinding.etLandUseType.getText().toString().trim());
        mRequest.setLand_area(mViewBinding.etLandArea.getText().toString().trim());
    }
}
